package com.ray.business.strategy.order.edit;

import cn.hutool.core.util.StrUtil;
import com.ray.business.builder.BusinessBuilder;
import com.ray.business.builder.BusinessInBuilder;
import com.ray.business.check.BusinessOutCheck;
import com.ray.business.enums.BusinessStatusEnum;
import com.ray.business.enums.BusinessTypeEnum;
import com.ray.business.enums.OrderStatusEnum;
import com.ray.business.service.ProdBusinessInService;
import com.ray.business.service.ProdBusinessOutService;
import com.ray.business.service.ProdBusinessService;
import com.ray.business.table.entity.ProdBusinessIn;
import com.ray.business.table.entity.ProdBusinessOut;
import com.ray.business.table.params.business.BusinessCreateParams;
import com.ray.business.table.params.business.BusinessEditParams;
import com.ray.common.SysMsgCodeConstant;
import com.ray.magicBlock.Strategy;
import com.ray.magicBlock.anno.Block;
import com.ray.woodencreate.beans.LoginUser;
import com.ray.woodencreate.exception.BusinessExceptionFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author bo shen
 * @Description: 订单商品数据
 * @Class: PurchaseInService
 * @Package com.ray.wms.strategy.in
 * @date 2020/6/8 15:05
 * @company <p>Ray快速开发平台</p>
 * @updateRecord time(修改时间)  author(修改人)   desc(修改内容)
 */
@Slf4j
@Block(group = "editBusiness", strategy = "in", desc = "工艺商品物料编辑")
public class InOrderEditService implements Strategy<BusinessEditParams, LoginUser, String> {

    @Autowired
    private ProdBusinessService prodBusinessService;
    @Autowired
    private ProdBusinessInService prodBusinessInService;
    @Autowired
    private ProdBusinessOutService prodBusinessOutService;

    @Override
    public String execute(BusinessEditParams editParams, LoginUser loginUser) {
        BusinessBuilder businessBuilder = new BusinessBuilder();
        businessBuilder.append(editParams).appendEdit(loginUser).appendStatus(BusinessStatusEnum.UN_CHECK.getValue());
        //还原对应出库的状态
        prodBusinessInService.listAll(editParams.getBusinessCode(), loginUser).forEach(prodBusinessIn -> {
            //还原数据
            if (StrUtil.isNotBlank(prodBusinessIn.getOutCode())) {
                prodBusinessOutService.cancelBusinessOut(prodBusinessIn.getOutCode(), loginUser);
            }
        });
        //删除原有的记录
        prodBusinessInService.deleteByBusinessCode(editParams.getBusinessCode(), loginUser);
        final List<BigDecimal> quantity = new ArrayList<>();

        //保存物料明细
        List<ProdBusinessIn> inList = editParams.getGoods().stream().map(businessGoodsParams -> {
            BusinessInBuilder businessInBuilder = new BusinessInBuilder().append(businessGoodsParams)
                    .appendBusinessCode(businessBuilder.getCode()).appendCreate(loginUser);
            //判断是否关联唯一单号
            if (StrUtil.isNotBlank(businessGoodsParams.getCode())) {
                //查询数据信息
                ProdBusinessOut businessOut = prodBusinessOutService.getUsableGoodsByCode(businessGoodsParams.getCode(), loginUser);
                new BusinessOutCheck(businessOut).checkNull(SysMsgCodeConstant.Error.ERR10000002);
                //更新使用状态
                if (!prodBusinessOutService.useBusinessOut(businessGoodsParams.getCode(), businessOut.getUpdateVersion(), loginUser)) {
                    log.info("更新记录失败:{}", businessGoodsParams.getCode());
                    throw BusinessExceptionFactory.newException("更新记录异常");
                }
                //添加关联
                businessInBuilder.appendOutCode(businessOut.getCode());
            }
            quantity.add(businessGoodsParams.getQuantity());
            return businessInBuilder.bulid();
        }).collect(Collectors.toList());
        //保存总数
        businessBuilder.append(quantity.stream().reduce(BigDecimal::add).get());
        //更新
        prodBusinessService.edit(businessBuilder.bulid(), loginUser);
        //保存数据
        prodBusinessInService.saveBatch(inList);
        return businessBuilder.getCode();
    }
}
